package cn.zhizihua.service.impl;

import cn.zhizihua.mapper.BusinessMapper;
import cn.zhizihua.pojo.Business;
import cn.zhizihua.query.BaseQuery;
import cn.zhizihua.query.BusinessQuery;
import cn.zhizihua.service.IBusinessService;
import cn.zhizihua.vo.PageBean;
import cn.zhizihua.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BusinessService implements IBusinessService {
    @Autowired  //自动注入
    BusinessMapper businessMapper;

    @Transactional  //spring事务处理
    @Override
    public ResultBean add(Business business) {
        //参数校验
        if (business == null ||
                StringUtils.isEmpty(business.getBusinessName()) ||
                StringUtils.isEmpty(business.getBusinessAddress())
        ){
            return ResultBean.ERROR(10001,"params null");
        }
        int add = businessMapper.add(business);
        return add>0?ResultBean.SUCCESS():ResultBean.ERROR("add business fail");
    }

    @Transactional
    @Override
    public ResultBean update(Business business) {
        //参数校验
        if (business == null ||
                StringUtils.isEmpty(business.getBusinessName()) ||
                StringUtils.isEmpty(business.getBusinessAddress()) ||
                business.getBusinessId() == null
        ){
            return ResultBean.ERROR(10001,"params null");
        }
        int update = businessMapper.update(business);
        return update>0?ResultBean.SUCCESS():ResultBean.ERROR("update business fail");
    }

    @Transactional
    @Override
    public ResultBean delete(Integer id) {
        if (id == null){
            return ResultBean.ERROR(10001,"params null");
        }
        int delete = businessMapper.delete(id);
        return delete>0?ResultBean.SUCCESS():ResultBean.ERROR("delete business fail");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getById(Integer id) {
        if (id == null){
            return ResultBean.ERROR(10001,"params null");
        }
        Business business = businessMapper.getById(id);
        return business!=null?ResultBean.SUCCESS(business):ResultBean.ERROR("getById business fail");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getAll() {
        List<Business> businessList = businessMapper.getAll();
        return businessList!=null?ResultBean.SUCCESS(businessList):ResultBean.ERROR("get all business fail");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getByPage(BaseQuery baseQuery) {
        if (baseQuery == null){
            baseQuery = new BusinessQuery();
        }

        PageBean<Business> pageBean = new PageBean<>();
        //查询总记录
        int count = businessMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);

        //计算总页数
        /*if(count% baseQuery.getPageCount()==0){
            pageBean.setTotalPages(count/baseQuery.getPageCount());
        }else{
            pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
        }*/

        //查询集合
        List<Business> businessList = businessMapper.getByPage(baseQuery);
        pageBean.setData(businessList);

        return ResultBean.SUCCESS(pageBean);
    }

    @Transactional
    @Override
    public ResultBean batchDel(List<Integer> ids){
        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("params null");
        }

        for (Integer id:ids){
            int delete = businessMapper.delete(id);
            if (delete < 1){
                throw new RuntimeException("delete business error");
            }
        }

        return ResultBean.SUCCESS();
    }
}
